<html>
<head>
	<script type="text/javascript">
		var aJaxURL	= "server-side/view/pricelist.action.php";		//server side folder url
		var seoyURL = "server-side/seoy/seoy.action.php";			//server side folder url
		var tName	= "example";									//table name
		var fName	= "add-edit-form";								//form name
		    	
		$(document).ready(function () {
			LoadTable();
			
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "delete_button");			
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);			
		});
		
        //SeoYyy
		$(document.body).click(function (e) {
        	$("#prod_name").autocomplete("close");
        });
        
		function LoadTable(){
			/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
			GetDataTable(tName, aJaxURL, "get_list", 6, "", 0, "", 1, "desc");
		}
		
		function LoadDialog(){
			/* Dialog Form Selector Name, Buttons Array */
			GetDialog(fName, 450, "auto", "");
			
			SeoY("prod_name", seoyURL, "pricelist_prod_name", "", 0);
		}
		
	    // Add - Save
	    $(document).on("click", "#save-dialog", function () {
	    	param		= new Object();

	    	param.act	= "save_prod";
		    
	    	param.id 	= $("#prod_id").val();
	    	param.n  	= $("#prod_name").val();
	    	param.tp 	= parseFloat($("#t_price").val());
	    	param.sp 	= $("#sp_price").val();
	    	param.sb 	= $("#sb_price").val();
			
			if(param.sb == ""){
				alert("შეავსეთ სალაროს ფასი!");
			}else if(param.sp == ""){
				alert("შეავსეთ დარბაზის ფასი!");
				}else {
				    $.ajax({
				        url: aJaxURL,
					    data: param,
				        success: function(data) {
							if(typeof(data.error) != "undefined")
							{
								if(data.error != "")
								{
									alert(data.error);
								}else
								{
									LoadTable();
						        	CloseDialog(fName);
								}
							}
					    }
				    });
				}
		});
		
	    function UpdateSelfPrice(e){
			param 			= new Object();

			param.act		= "get_price";
			param.p_name	= e;	 
		    
		    $.ajax({
		        url: aJaxURL,
			    data: param,
		        success: function(data) {
					if(typeof(data.error) != "undefined")
					{
						if(data.error != "")
						{
							alert(data.error);
						}else
						{
							$("#t_price").val(data.self_price);
							$("#l_price").val(data.last_price);
						}
					}
			    }
		    });
		}
		
	    function CalculateFromPercent(e){
		    var l_price  	= parseFloat($("#l_price").val());
		    var percent	 	= parseFloat(e);
		    
		    var sum			= l_price + l_price * percent / 100;
		    
	    	$("#sp_price").val(Number((sum).toFixed(2)));
	    	
	    }
	    
	    function CalculatePercent(e){
			var price   	= parseFloat($("#sp_price").val());
			var l_price  	= parseFloat($("#l_price").val());

			//var percent     = price * 100 / l_price - 100;
			var percent		= (100*(price - l_price))/l_price;

			//Number((percent).toFixed(2));
	    	$("#percent").val(Number((percent).toFixed(2)));
	    }
	    
		//Register Enter Effect
		$(document).on("keydown", "#prod_name", function (event) {
			if (event.keyCode == $.ui.keyCode.ENTER){
				UpdateSelfPrice(this.value);
				$("#percent").focus();
            }
		});
		
		//Calculate percent
		$(document).on("keydown", "#percent", function (event) {
			if (event.keyCode == $.ui.keyCode.ENTER){
				CalculateFromPercent(this.value);
            }
		});
		
		//Calculate percent
		$(document).on("keydown", "#sp_price", function (event) {
			if (event.keyCode == $.ui.keyCode.ENTER){
				CalculatePercent(this.value);
            }
		});
		
		//--SeoY
	    $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
		});

		$(document).on("keyup", "#percent, #sp_price, #sb_price", function() {
		    var val = $(this).val();
		    if(isNaN(val)){
		    	alert("მოცემულ ველში შეიყვანეთ მხოლოდ ციფრები");
		         val = val.replace(/[^0-9\.]/g,'');
		         if(val.split('.').length>2) 
		             val =val.replace(/\.+$/,"");
		    }
		    $(this).val(val); 
		});

    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">ფასების სია</h2>
            	 <div id="button_area">
        			<button id="add_button">დამატება</button>
        			<button id="delete_button">წაშლა</button>
        		</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min">შტრიხკოდი</th>
                            <th style="width: 100%;">დასახელება</th>
                            <th class="min">თვით ღირებულება</th>
                           <th class="min">საბითუმო ფასი</th>
                            <th class="min">საცალო ფასი</th>
                          	<th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="პროდუქციის ფასები">
    	<!-- aJax -->
	</div>
</body>
</html>